<template>
    <div class="edit-popup">
        <popup
            ref="popupRef"
            :title="popupTitle"
            :async="true"
            width="550px"
            :clickModalClose="true"
            @confirm="handleSubmit"
            @close="handleClose"
        >
            <el-form ref="formRef" :model="formData" label-width="84px" :rules="formRules">
                <el-form-item label="主键ID" prop="id">
                    <el-input v-model="formData.id" placeholder="请输入主键ID" />
                </el-form-item>
                <el-form-item label="黑名单客户" prop="blacklistedCustomer">
                    <el-input
                        v-model="formData.blacklistedCustomer"
                        placeholder="请输入黑名单客户"
                    />
                </el-form-item>
                <el-form-item label="黑名单客户(自命名)" prop="blacklistedNelvolt">
                    <el-input
                        v-model="formData.blacklistedNelvolt"
                        placeholder="请输入黑名单客户(自命名)"
                    />
                </el-form-item>
                <el-form-item label="黑名单类型" prop="blacklistedType">
                    <el-select
                        class="flex-1"
                        v-model="formData.blacklistedType"
                        placeholder="请选择黑名单类型"
                    >
                        <el-option label="请选择字典生成" value="" />
                    </el-select>
                </el-form-item>
                <el-form-item label="利益冲突客户来源" prop="conflictInterestClientSources">
                    <el-input
                        v-model="formData.conflictInterestClientSources"
                        placeholder="请输入利益冲突客户来源"
                    />
                </el-form-item>
                <el-form-item label="限制禁止结案分公司" prop="restrictionsBranchCompanies">
                    <el-input
                        v-model="formData.restrictionsBranchCompanies"
                        placeholder="请输入限制禁止结案分公司"
                    />
                </el-form-item>
                <el-form-item label="利益冲突要求" prop="conflictInterestRequirement">
                    <el-input
                        v-model="formData.conflictInterestRequirement"
                        placeholder="请输入利益冲突要求"
                    />
                </el-form-item>
                <el-form-item label="来源" prop="source">
                    <el-input v-model="formData.source" placeholder="请输入来源" />
                </el-form-item>
                <el-form-item label="是否显示" prop="isShow">
                    <el-input v-model="formData.isShow" placeholder="请输入是否显示" />
                </el-form-item>
                <el-form-item label="创建用户" prop="createsUser">
                    <el-input v-model="formData.createsUser" placeholder="请输入创建用户" />
                </el-form-item>
                <el-form-item label="创建时间" prop="createsTime">
                    <el-input v-model="formData.createsTime" placeholder="请输入创建时间" />
                </el-form-item>
                <el-form-item label="更新用户" prop="updatesUser">
                    <el-input v-model="formData.updatesUser" placeholder="请输入更新用户" />
                </el-form-item>
            </el-form>
        </popup>
    </div>
</template>
<script lang="ts" setup>
import type { FormInstance } from 'element-plus'
import {
    competitionrelevanceEdit,
    competitionrelevanceAdd,
    competitionrelevanceDetail
} from '@/api/competitionrelevance'
import Popup from '@/components/popup/index.vue'
import feedback from '@/utils/feedback'
import type { PropType } from 'vue'
defineProps({
    dictData: {
        type: Object as PropType<Record<string, any[]>>,
        default: () => ({})
    }
})
const emit = defineEmits(['success', 'close'])
const formRef = shallowRef<FormInstance>()
const popupRef = shallowRef<InstanceType<typeof Popup>>()
const mode = ref('add')
const popupTitle = computed(() => {
    return mode.value == 'edit' ? '编辑竞业关联' : '新增竞业关联'
})

const formData = reactive({
    id: '',
    blacklistedCustomer: '',
    blacklistedNelvolt: '',
    blacklistedType: '',
    conflictInterestClientSources: '',
    restrictionsBranchCompanies: '',
    conflictInterestRequirement: '',
    source: '',
    isShow: '',
    createsUser: '',
    createsTime: '',
    updatesUser: ''
})

const formRules = {}

const handleSubmit = async () => {
    await formRef.value?.validate()
    const data: any = { ...formData }
    mode.value == 'edit'
        ? await competitionrelevanceEdit(data)
        : await competitionrelevanceAdd(data)
    popupRef.value?.close()
    feedback.msgSuccess('操作成功')
    emit('success')
}

const open = (type = 'add') => {
    mode.value = type
    popupRef.value?.open()
}

const setFormData = async (data: Record<string, any>) => {
    for (const key in formData) {
        if (data[key] != null && data[key] != undefined) {
            //@ts-ignore
            formData[key] = data[key]
        }
    }
}

const getDetail = async (row: Record<string, any>) => {
    const data = await competitionrelevanceDetail({
        id: row.id
    })
    setFormData(data)
}

const handleClose = () => {
    emit('close')
}

defineExpose({
    open,
    setFormData,
    getDetail
})
</script>
